package com.rent.utils;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.util.Iterator;
import java.util.List;
import java.util.Scanner;

import com.rent.bean.Brand;
import com.rent.bean.Car;
import com.rent.bean.Rent;
import com.rent.bean.Type;
import com.rent.bean.User;

/**
 * 视图层提供工具
 * @author xin
 *
 */
public class ViewUtils {
	Scanner sn = new Scanner(System.in);
	public void printTitle(String title) {
		System.out.println("---------------"+title+"---------------");
	}
	/**
	 * 展示汽车信息
	 * @param l
	 */
	public void show_car(List<Car> l) {
		if(!l.isEmpty()) {
			System.out.println("编号\t品牌\t价格\t车型\t颜色\t变速箱\t座位数\t车牌号\t等级\t发动机编号\t押金\t标定容积（升）\t实际装油（升）\t汽油编号");
			for(Car c:l) {
				System.out.println(
					c.getCarid()+"\t"+
					c.getCbrand()+"\t"+
					c.getCprice()+"\t"+
					c.getCtype()+"\t"+
					c.getColor()+"\t"+
					c.getGearbox()+"\t"+
					c.getSeat()+"\t"+
					c.getCarNumber()+"\t"+
					c.getCgrade()+"\t"+
					c.getEngineNumber()+"\t"+
					c.car_deposit+"\t"+
					c.getGasolineVolume()+"\t"+
					c.getGasolineActualVolume()+"\t"+
					c.getGasolineNumber()
				);
			}
		}else {
			System.out.println("没有车辆信息!");
		}
	}
	
	public void show_car_type(List<Car> l) {
		if(!l.isEmpty()) {
			//品牌-型号-座位-变速箱-等级-油箱容量-现有油量-汽油型号-价格-车牌号
			System.out.println("编号\t品牌\t型号\t颜色\t座位\t变速箱\t等级\t油箱容量\t现有油量\t汽油型号\t价格\t车牌号");
			for(Car c : l) {
				System.out.println(
					c.getCarid()+"\t"+
					c.getCbrand()+"\t"+
					c.getCtype()+"\t"+
					c.getColor()+"\t"+
					c.getSeat()+"\t"+
					c.getGearbox()+"\t"+
					c.getCgrade()+"\t"+
					c.getGasolineVolume()+"\t"+
					c.getGasolineActualVolume()+"\t"+
					c.getGasolineNumber()+"\t"+
					c.getCprice()+"\t"+
					c.getCarNumber()
				);
			}
		}
	}
	
	public void show_car_admin(List<Car> l) {
		if(!l.isEmpty()) {
			System.out.println("编号\t品牌\t价格\t车型\t颜色\t变速箱\t座位数\t车牌号\t是否删除\t等级\t状态\t发动机编号\t押金\t标定容积（升）\t实际装油（升）\t汽油编号");
			for(Car c:l) {
				System.out.println(
					c.getCarid()+"\t"+
					c.getCbrand()+"\t"+
					c.getCprice()+"\t"+
					c.getCtype()+"\t"+
					c.getColor()+"\t"+
					c.getGearbox()+"\t"+
					c.getSeat()+"\t"+
					c.getCarNumber()+"\t"+
					c.getIsdelete()+"\t"+
					c.getCgrade()+"\t"+
					c.getStatus_detailed()+"\t"+
					c.getEngineNumber()+"\t"+
					c.car_deposit+"\t"+
					c.getGasolineVolume()+"\t"+
					c.getGasolineActualVolume()+"\t"+
					c.getGasolineNumber()
					
				);
			}
		}else {
			System.out.println("没有车辆信息！");
		}
	}
	/**
	 * 展示订单信息
	 * @param l
	 */
	public void show_rent(List<Rent> l) {
		if(!l.isEmpty()) {
			System.out.println("编号\t用户名\t取车时间\t还车时间\t支付金额\t状态\t汽车押金\t违章押金\t汽车押金状态\t汽车押金描述\t违章押金状态\t违章押金描述\t汽车编号\t品牌\t型号\t颜色\t座位数\t变速箱\t车牌号\t油箱容量\t实际油箱容量\t汽油编号\t价格/小时\t状态\t等级");
			for(Rent r : l) {
				String car_deposit_status = r.getCar_deposit_status()==0?"未退还":"已退还";
				String violation_deposit_status = r.getViolation_deposit_status()==0?"未退还":"已退还";
				String violation_deposit_describe = r.getViolation_deposit_describe()==null?"无":r.getViolation_deposit_describe();
				String car_deposit_describe = r.getCar_deposit_describe()==null?"无":r.getCar_deposit_describe();
				System.out.println(
					r.getRid()+"\t"+
					r.getUname()+"\t"+
					r.getRtime()+"\t"+
					r.getBtime()+"\t"+
					r.getMoney()+"\t"+
					r.getStatus_detailed()+"\t"+
					r.getCar_deposit()+"\t"+
					r.getViolation_deposit()+"\t"+
					car_deposit_status+"\t"+
					car_deposit_describe+"\t"+
					violation_deposit_status+"\t"+
					violation_deposit_describe+"\t"+
					r.c.getCarid()+"\t"+
					r.c.getCbrand()+"\t"+
					r.c.getCtype()+"\t"+
					r.c.getColor()+"\t"+
					r.c.getSeat()+"\t"+
					r.c.getGearbox()+"\t"+
					r.c.getCarNumber()+"\t"+
					r.c.getGasolineVolume()+"\t"+
					r.c.getGasolineActualVolume()+"\t"+
					r.c.getGasolineNumber()+"\t"+
					r.c.getCprice()+"\t"+
					r.c.getStatus_detailed()+"\t"+
					r.c.getCgrade()
				);
				//\t汽车编号\t品牌\t型号\t座位数\t变速箱\t车牌号\t油箱容量\t实际油箱容量\t汽油编号\t价格/小时\t状态\t等级
			}
		}else {
			System.out.println("您没有租赁信息!");
		}
	}
	
	public void show_user(List<User> l) {
		if(!l.isEmpty()) {
			System.out.println("编号\t用户名\t用户\t类型\t真实姓名\t身份证号码\t电话\t余额\t身份是否确认");
			for (User u : l) {
				String confirminfo = u.getConfirminfo()==0?"否":"是";
				System.out.println(
					u.getUid()+"\t"+
					u.getUname()+"\t"+
					u.getPower()+"\t"+
					u.getIdname()+"\t"+
					u.getIdcard()+"\t"+
					u.getPhonenumber()+"\t"+
					u.getBalance()+"\t"+
					confirminfo
				);
			}
		}else {
			System.out.println("没有用户信息");
		}
	}
	/**
	 * 订单展示
	 * @param r
	 * @param c
	 */
	public void show_rent(Rent r,Car c) {
		String car_deposit = r.getCar_deposit_status()==1?"已退还":"未退还";
		String violation_deposit= r.getViolation_deposit_status()==1?"已退还":"未退还";
		String violation_deposit_describe = r.getViolation_deposit_describe()==null?"无":r.getViolation_deposit_describe();
		String car_deposit_describe = r.getCar_deposit_describe()==null?"无":r.getCar_deposit_describe();
		System.out.println("*********汽车信息********");
		System.out.println("品牌："+c.getCbrand()+"\r\n" + 
				"型号："+c.getCtype()+"\r\n" + 
				"颜色："+c.getColor()+"\r\n" + 
				"座位数："+c.getSeat()+"\r\n" + 
				"挡位："+c.getGearbox()+"\r\n" + 
				"车牌号："+c.getCarNumber()+"\r\n" + 
				"发动机编号："+c.getEngineNumber()+"\r\n" + 
				"油箱容量："+c.getGasolineVolume()+"\r\n" + 
				"实际油箱容量："+c.getGasolineActualVolume()+"\r\n" + 
				"汽车适用汽油号："+c.getGasolineNumber()+"\r\n" + 
				"汽车状态："+c.getStatus_detailed());
		System.out.println("*********租赁信息********");
		System.out.println("租赁用户："+r.getUname()+" "+r.getStatus_detailed()+"\r\n" + 
				"取车时间："+r.getRtime()+"\r\n" + 
				"还车时间："+r.getBtime()+"\r\n" + 
				"租赁金额："+r.getMoney()+"￥\r\n" + 
				"汽车押金："+r.getCar_deposit()+"￥ "+car_deposit+" "+car_deposit_describe+" \r\n" + 
				"违章押金："+r.getViolation_deposit()+"￥ "+violation_deposit+" "+violation_deposit_describe);
		System.out.println("是否打印(y/n)");
		switch (sn.next()) {
		case "y":
			Writer w = null;
			try {
				w = new FileWriter(r.getUname()+".txt");
			} catch (IOException e) {
				e.printStackTrace();
			}
			BufferedWriter bw = new BufferedWriter(w);
			try {
				bw.write("*********汽车信息********");
				bw.newLine();
				bw.write("品牌："+c.getCbrand()+"\r\n" + 
				"型号："+c.getCtype()+"\r\n" + 
				"颜色："+c.getColor()+"\r\n" + 
				"座位数："+c.getSeat()+"\r\n" + 
				"挡位："+c.getGearbox()+"\r\n" + 
				"车牌号："+c.getCarNumber()+"\r\n" + 
				"发动机编号："+c.getEngineNumber()+"\r\n" + 
				"油箱容量："+c.getGasolineVolume()+"\r\n" + 
				"实际油箱容量："+c.getGasolineActualVolume()+"\r\n" + 
				"汽车适用汽油号："+c.getGasolineNumber()+"\r\n" + 
				"汽车状态："+c.getStatus_detailed());
				bw.newLine();
				bw.write("*********租赁信息********");
				bw.newLine();
				bw.write("租赁用户："+r.getUname()+" "+r.getStatus_detailed()+"\r\n" + 
						"取车时间："+r.getRtime()+"\r\n" + 
						"还车时间："+r.getBtime()+"\r\n" + 
						"租赁金额："+r.getMoney()+"￥\r\n" + 
						"汽车押金："+r.getCar_deposit()+"￥ "+car_deposit+" "+car_deposit_describe+" \r\n" + 
						"违章押金："+r.getViolation_deposit()+"￥ "+violation_deposit+" "+violation_deposit_describe);
				bw.newLine();
				System.out.println("打印完成");
			} catch (IOException e) {
				e.printStackTrace();
			} finally {
				try {
					bw.close();
					w.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			break;
		}
	}
	public void show_user_rent(List<User> sele) {
		if(!sele.isEmpty()) {
			System.out.println("编号\t用户名\t用户\t类型\t真实姓名\t身份证号码\t电话\t余额\t身份是否确认\t租车记录数量");
			for (User u : sele) {
				String confirminfo = u.getConfirminfo()==0?"否":"是";
				System.out.println(
					u.getUid()+"\t"+
					u.getUname()+"\t"+
					u.getPower()+"\t"+
					u.getIdname()+"\t"+
					u.getIdcard()+"\t"+
					u.getPhonenumber()+"\t"+
					u.getBalance()+"\t"+
					confirminfo+"\t"+
					u.getRentCount()	
				);
			}
		}else {
			System.out.println("没有用户信息");
		}
	}
	public void show_rent(Rent r) {
		if(r!=null) {
			System.out.println("编号\t用户名\t取车时间\t还车时间\t支付金额\t状态\t汽车押金\t违章押金\t汽车押金状态\t汽车押金描述\t违章押金状态\t违章押金描述\t汽车编号\t品牌\t型号\t颜色\t座位数\t变速箱\t车牌号\t油箱容量\t实际油箱容量\t汽油编号\t价格/小时\t状态\t等级");
			String car_deposit_status = r.getCar_deposit_status()==0?"未退还":"已退还";
			String violation_deposit_status = r.getViolation_deposit_status()==0?"未退还":"已退还";
			String violation_deposit_describe = r.getViolation_deposit_describe()==null?"无":r.getViolation_deposit_describe();
			String car_deposit_describe = r.getCar_deposit_describe()==null?"无":r.getCar_deposit_describe();
			System.out.println(
				r.getRid()+"\t"+
				r.getUname()+"\t"+
				r.getRtime()+"\t"+
				r.getBtime()+"\t"+
				r.getMoney()+"\t"+
				r.getStatus_detailed()+"\t"+
				r.getCar_deposit()+"\t"+
				r.getViolation_deposit()+"\t"+
				car_deposit_status+"\t"+
				car_deposit_describe+"\t"+
				violation_deposit_status+"\t"+
				violation_deposit_describe+"\t"+
				r.c.getCarid()+"\t"+
				r.c.getCbrand()+"\t"+
				r.c.getCtype()+"\t"+
				r.c.getColor()+"\t"+
				r.c.getSeat()+"\t"+
				r.c.getGearbox()+"\t"+
				r.c.getCarNumber()+"\t"+
				r.c.getGasolineVolume()+"\t"+
				r.c.getGasolineActualVolume()+"\t"+
				r.c.getGasolineNumber()+"\t"+
				r.c.getCprice()+"\t"+
				r.c.getStatus_detailed()+"\t"+
				r.c.getCgrade()
			);
		}else {
			System.out.println("您没有租赁信息!");
		}
	}
	public void show_user_vip(List<User> l) {
		if(!l.isEmpty()) {
			System.out.println("编号\t用户名\t类型\t真实姓名\t身份证号码\t电话\t会员充值时间\t到期时间");
			for (User u : l) {
				System.out.println(
					u.getUid()+"\t"+
					u.getUname()+"\t"+
					u.getIdname()+"\t"+
					u.getIdcard()+"\t"+
					u.getPhonenumber()+"\t"+
					u.vip.getOpentime()+"\t"+
					u.vip.getLimittime()
				);
			}
		}else {
			System.out.println("没有用户信息");
		}
	}
	/**
	 * 汽车品牌
	 * @param seleByBrand
	 */
	public void show_brand(List<Brand> l) {
		if(l.isEmpty()) {
			System.out.println("没有品牌信息");
			return;
		}
		System.out.println("编号\t品牌");
		for(Brand b : l) {
			System.out.println(b.getBid()+"\t"+b.getBrand());
		}
	}
	/**
	 * 汽车类型
	 * @param seleByType
	 */
	public void show_type(List<Type> l) {
		if(l.isEmpty()) {
			System.out.println("没有类型信息");
			return;
		}
		System.out.println("编号\t品牌\t类型\t价格");
		for(Type t : l) {
			System.out.println(t.getTid()+"\t"+t.getBrand()+"\t"+t.getType()+"\t"+t.getPrice());
		}
	}
}
